package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Service;
import java.util.List;

public class UserContext {
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    /**
     * 获得session对象
     * @return
     */
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    /**
     * 从session中获得当前用户
     * @return
     */
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    /**
     * 把用户数据设值进session中
     * @param employee
     */
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    /**
     * 从session中获得当前用户权限
     * @return
     */
    public static List<String > getPermission(){
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }

    /**
     * 把权限存进进session中
     * @param permission
     */
    public static void setPermission(List<String> permission){
        getSession().setAttribute(PERMISSION_IN_SESSION,permission);
    }


}
